﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Cci;

using Logging;

using PartialExpressionCompleter.Util;

namespace PartialExpressionCompleter.Experiment
{
    class CountExperiment : Experiment
    {
        public override string FileExtension
        {
            get { return "alltypes"; }
        }

        public CountExperiment(Microsoft.Cci.MutableCodeModel.Assembly unit)
            : base(unit)
        { }

        public override void RunExperiment()
        {
            foreach (var type in CCIUtil.GetAllTypes())
            {
                Logger.PrintLine("Type: " + type.ToMangledString());
            }
            foreach (var ns in CCIUtil.GetAllNamespaces())
            {
                Logger.PrintLine("NS: " + ns.ToString());
            }
            foreach (var meth in CCIUtil.GetAllMethods())
            {
                Logger.PrintLine("Method: " + meth.ToString());
            }
            /*foreach (var typeGroup in
                CCIUtil.GetAllTypes()
                    .GroupBy(t => t.GetContainingUnit()))
            {
                Logger.PrintLine("Group unit: " + typeGroup.Key);
                Logger.PrintLine("Type count: " + typeGroup.Count());
                Logger.PrintLine();
            }*/
        }
    }

    static class TypeUnitUtil
    {
        public static IUnit GetContainingUnit(this ITypeDefinition type)
        {
            if (type is INamespaceTypeDefinition)
            {
                return (type as INamespaceTypeDefinition).ContainingUnitNamespace.Unit;
            }
            else if (type is INestedTypeDefinition)
            {
                var nt = type as INestedTypeDefinition;
                return nt.ContainingTypeDefinition.GetContainingUnit();
            }
            else return null;
        }
    }
}
